package com.atguigu.dga.governance.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga.governance.bean.GovernanceAssessTecOwner;
import com.atguigu.dga.governance.mapper.GovernanceAssessTecOwnerMapper;
import com.atguigu.dga.governance.service.GovernanceAssessTecOwnerService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 技术负责人治理考评表 服务实现类
 * </p>
 *
 * @author liuxu
 * @since 2023-10-16
 */
@Service
@DS("dga")
public class GovernanceAssessTecOwnerServiceImpl extends ServiceImpl<GovernanceAssessTecOwnerMapper, GovernanceAssessTecOwner> implements GovernanceAssessTecOwnerService {

    @Override
    public void calcTecOwnerScore(String assessDate) {
        //幂等处理
        remove(
                new QueryWrapper<GovernanceAssessTecOwner>().eq("assess_date",assessDate)
        );

        List<GovernanceAssessTecOwner> governanceAssessTecOwners = getBaseMapper().selectGovernanceAssessTecOwner(assessDate);
        //批写
        saveBatch(governanceAssessTecOwners);
    }

    @Override
    public String getRankListService() {
        List<GovernanceAssessTecOwner> list = list(
                new QueryWrapper<GovernanceAssessTecOwner>()
                        .select("tec_owner", "score")
                        .inSql("assess_date", "select max(assess_date) from governance_assess_detail")
                        .orderByDesc("score")
        );
        return JSONObject.toJSONString(list);
    }
}
